import multiprocessing
import os
import time

'''
def task_process(delay):
     num = 0
     for i in range(delay*100000000):
          num+=i
     print(f"进程pid为{os.getpid()},执行完成")


if __name__ =='__main__':
     print('父进程pid为%s。' % os.getpid())
     t0 = time.time()
     task_process(3)
     task_process(3)
     t1=time.time()
     print(f"顺序执行顺序{t1-t0}")
     p0=Process(target=task_process, args=(3,))
     p1=Process(target=task_process, args=(4,))
     t2=time.time()
     p0.start();p1.start()
     p0.join();p1.join()
     t3 = time.time()
     print(f"多进程并发执行耗时{t3-t2}")


class MyProcess(Process):
    def __init__(self, delay):
        self.delay = delay
        super().__init__()

    def run(self):
        num = 0
        for i in range(self.delay*100000000):
            num+=i
        print(f"进程pid为{os.getpid()},执行完成")


if __name__=="__main__":
    print("父进程pid为%s。"%os.getpid())
    p0 = MyProcess(3)
    p1 = MyProcess(3)
    t0 = time.time()
    p0.start()
    p1.start()
    p0.join()
    p1.join()
    t1 = time.time()
    print(f"多进程并发执行耗时{t1-t0}")

'''


def woker(s, i):
    s.acquire()
    print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name+"获得锁运行")
    time.sleep(i)
    print(time.strftime('%H:%M:%S'), multiprocessing.current_process().name + "释放锁结束")
    s.release()


if __name__ == "__main__":
    s = multiprocessing.Semaphore(2)
    for i in range(6):
        p = multiprocessing.Process(target=woker, args=(s, 2))
        p.start()